// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

let { twentyFourGenerator } = require('./24')
let bestAnswer = ""
let clickStack = new Array();
cc.Class({
    extends: cc.Component,

    properties: {
        scoreLabel:cc.Label,
        alertView:cc.Node,
        operatorView:cc.Node,
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        
        this.scoreLabel.string = "关卡0"
        //将随机数渲染到四个按钮上
        this.init()
        // this.showAnswerAlert()
        // console.log(bestAnswer)
    },

    start () {

    },
    // 获取随机数与最佳答案
    init(){
        
        twentyFourGenerator.getBestAnswer()
        bestAnswer = twentyFourGenerator.bestAnswer
        this.flush()
    },
    flush(){
        let count= 0;
        for(let i=0;i<this.node.children.length;i++){
            let x = this.node.children[i]
        // this.node.children.forEach(x=>{
            if(count>3) break;
            let number = twentyFourGenerator.randomNumArray[count++]
            let str = number+""
            x.children[0].getComponent(cc.Label).string = str
            x._isNumber = true
            x._number = number
        // })
        }

        for(let i =4;i<=7;i++){
            this.node.children[i]._isNumber = false; 
        }
        //设置每个按钮的按压状态为为按压
        // this.node.children.forEach(x=>{
        //     x._isSelected = false;
        // })
        for(let i=0;i<this.node.children.length;i++){
            this.node.children[i]._isSelected = false
        }
        //初始化颜色
        for(let i=0;i<7;i++){
            this.node.children[i].color = new cc.Color(2, 159, 218);
        }
        //清空运算栈
        clickStack = []

        
        //弹出框重置
        this.closeAnswerAlert()
        //重置按钮为重置
        this.operatorView.getChildByName('reset')._isReset = true;
        // console.log(this.operatorView)
    },
    btnClick(target,flag){
        
        // console.log(flag)
        let _node = target.target;
        
        // console.log(_node)
        if(_node._isSelected==true){
            this.changeColor(_node)
            clickStack.pop()
            return;
        }
        if(clickStack.length==0){
            if(_node._isNumber==true){
                this.changeColor(_node)
                clickStack.push(_node)
                
            }
        }
        if(clickStack.length==1){
            if(_node._isNumber==false){
                this.changeColor(_node)
                clickStack.push(_node)
            }
        }
        if(clickStack.length==2){
            if(_node._isNumber==true){
                this.changeColor(_node)
                clickStack.push(_node)
            }
        }
        if(clickStack.length==3){

            // console.log(clickStack)
            let arg1 = clickStack[0]._number
            let arg2 = clickStack[2]._number
            let operator = clickStack[1].children[0].getComponent(cc.Label).string
            // if(operator=="x") operator="*"
            // if(operator=="÷") operator="/"
            let result = twentyFourGenerator.arithmetic(arg1,arg2,operator)
            // console.log(result)
            const node1 = clickStack.shift()
            const node2 = clickStack.shift()
            const node3 = clickStack.shift()
            node1.active = false
            this.changeColor(node2)
            this.changeColor(node3)
            // clickStack[0]._number = result
            // clickStack[0].children[0].getComponent(cc.Label).string = this.fixedTwoToShow(result)
            node3._number = result
            node3.children[0].getComponent(cc.Label).string = this.fixedTwoToShow(result)
            // console.log(this.isLastStep())
            if(this.isLastStep()){
                //运算的结果是否为24
                if(result == 24){
                    //积分更新
                    let gateCount = parseInt(this.scoreLabel.string.substr(2)) 
                    this.scoreLabel.string = "关卡"+(++gateCount)
                    this.operatorView.getChildByName('reset')._isReset = false;
                    this.operatorView.getChildByName('reset').getChildByName('resetLabel').getComponent(cc.Label).string = "下一关"
                    //参考答案关掉
                    this.closeAnswerAlert()
                    //按钮变成重置
                    // this.operatorView.getChildByName('reset')._isReset = true
                    // this.operatorView.getChildByName('reset').getChildByName('resetLabel').getComponent(cc.Label).string = "重置"
                }else{
                    //没算出24
                    
                }
            }
        }
    },
    //非整数保留两位小数
    fixedTwoToShow(value){
        let result = value
        if(value != parseInt(value)){
            result = value.toFixed(2)
        }
        return result
    },
    //改变颜色
    changeColor(target){
        target._isSelected = !target._isSelected;
        if(target._isSelected == true){
            target.color = new cc.Color(255, 255, 255);
        }else{
            target.color = new cc.Color(2, 159, 218);
        }
    },
    //是否计算到了最后一步
    isLastStep(){
        let count = 4
        for(let i=0;i<4;i++){
            if(this.node.children[i].active==false){
                count--
            }
            if(count==1){
                return true
            }
        }
        return false
    },
    reset(target){
        let _node = target.target
        if(_node._isReset){
            // this.node.children.forEach(x=>{
            //     x.active = true
            // })
            for(let i=0;i<this.node.children.length;i++){
                this.node.children[i].active = true
            }
            this.flush()
        }else{
            //下一关
            // console.log("下一关的逻辑")
            this.operatorView.getChildByName('reset')._isReset = true
            this.operatorView.getChildByName('reset').getChildByName('resetLabel').getComponent(cc.Label).string = "重置"
            this.init()
            // this.node.children.forEach(x=>{
            //     x.active = true
            // })
            for(let i=0;i<this.node.children.length;i++){
                this.node.children[i].active = true
            }
        }
    },
    showAnswerAlert(){
        this.alertView.active = true
        // console.log(this.alertView.getChildByName("label").getComponent(cc.Label).string)
        this.alertView.getChildByName("label").getComponent(cc.Label).string = bestAnswer
    },
    closeAnswerAlert(){
        this.alertView.active = false
    }
    // update (dt) {},
});
